package com.atfusu.controller;

import com.atfusu.bean.entity.*;
import com.atfusu.bean.vo.HouseQueryVo;
import com.atfusu.bean.vo.HouseVo;
import com.atfusu.result.Result;
import com.atfusu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/12/2
 * Description:
 **/
@Controller
@RequestMapping("/house")
@ResponseBody
public class HouseController {

    @DubboReference
    private HouseService houseService;

    @DubboReference
    private CommunityService communityService;

    @DubboReference
    private HouseBrokerService houseBrokerService;

    @DubboReference
    private HouseImageService houseImageService;

    @DubboReference
    private UserFollowService userFollowService;

    @RequestMapping("/list/{pageNum}/{pageSize}")

    public Result list(@PathVariable Integer pageNum,
                       @PathVariable Integer pageSize,
                       @RequestBody HouseQueryVo houseQueryVo) {

        PageInfo<HouseVo> page = houseService.findListPage(pageNum, pageSize, houseQueryVo);

        return Result.ok(page);
    }


    @RequestMapping("/info/{houseId}")
    public Result info(@PathVariable Long houseId, HttpSession session) {
        //house对象
        House house = houseService.showById(houseId);
        //community对象
        Community community = communityService.showById(house.getCommunityId());
        //houseBroker对象
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(houseId);
        //houseImage1List
        List<HouseImage> HouseImageList = houseImageService.findListByHouseIdAndType(houseId, 1);
        //isFollow
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        Boolean isFollow = false;
        //当用户处于登录状态时进入if，并进行下面判断
        if (userInfo != null) {
            //还要根据userFollow查询是否关注该房源(等会做)
            isFollow = userFollowService.isFollow(houseId, userInfo.getId());
        }

        Map<String, Object> map = new HashMap<>();
        map.put("house", house);
        map.put("community", community);
        map.put("houseBrokerList", houseBrokerList);
        map.put("houseImage1List", HouseImageList);
        map.put("isFollow", isFollow);
        return Result.ok(map);
    }
}
